package Practicas.practica4;


import data.IntegerDataSetGenerator;
import impl.*;
import test.BenchMark;

import java.util.ArrayList;
import java.util.Collections;

public class TestSorters {

    private ArrayList<Integer> list;

    private void testMiliTime(int valor, int sort) {
        list = new ArrayList<Integer>(valor);
        IntegerDataSetGenerator integerDataSetGenerator = new IntegerDataSetGenerator();

        QuickSortWithCut quickSortWithCut = new QuickSortWithCut();
        QuickSorter quickSorter = new QuickSorter();
        QuickSorterMedianOfThree quickSorterMedianOfThree = new QuickSorterMedianOfThree();
        QuickSortNonRecursive quickSortNonRecursive = new QuickSortNonRecursive();
        QuickSortThreeWayPartition quickSortThreeWayPartition = new QuickSortThreeWayPartition();

        BenchMark benchMark = new BenchMark();
        quickSortWithCut.addSorterListener(benchMark);
        quickSorter.addSorterListener(benchMark);
        quickSorterMedianOfThree.addSorterListener(benchMark);
        quickSortNonRecursive.addSorterListener(benchMark);
        quickSortThreeWayPartition.addSorterListener(benchMark);

        float random;
        random = 0;
        if (!list.isEmpty()) {
            list.clear();
        } else {
            list = (ArrayList<Integer>) integerDataSetGenerator.createAscending(valor);
//            Collections.shuffle(list);
        }
        long tiempoRan1, tiempoRan2;
        int swapsRamdon, comparationsRandom;
        String nameSorter = "";
        tiempoRan1 = tiempoRan2 = swapsRamdon = comparationsRandom = 0;
        switch (sort) {
            case 0:
                tiempoRan1 = System.currentTimeMillis();
                quickSortWithCut.sort(integerDataSetGenerator.getComparator(), list, 9);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="quickSortWithCut";
                break;
            case 1:
                tiempoRan1 = System.currentTimeMillis();
                quickSorter.sort(integerDataSetGenerator.getComparator(), list);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="quickSorter";
                break;
            case 2:
                tiempoRan1 = System.currentTimeMillis();
                quickSorterMedianOfThree.sort(integerDataSetGenerator.getComparator(), list);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="MedianOfThree";
                break;
            case 3:
                tiempoRan1 = System.currentTimeMillis();
                quickSortNonRecursive.sort(integerDataSetGenerator.getComparator(), list);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="NonRecursive";
                break;
            case 4:
                tiempoRan1 = System.currentTimeMillis();
                quickSortThreeWayPartition.sort(integerDataSetGenerator.getComparator(), list);
                tiempoRan2 = System.currentTimeMillis();
                random = tiempoRan2 - tiempoRan1;
                swapsRamdon = benchMark.getnSwap();
                comparationsRandom = benchMark.getComparations();
                nameSorter="ThreeWayPartition";
                break;
        }

        System.out.println("||  Cant: " + valor + "  ||  Tiempo " + random + "  ||"+"nombre: "+nameSorter);
        benchMark.resetAll();

    }

    public static void main(String[] args) {

        TestSorters ts = new TestSorters();
        for (int i = 1000; i < 1000001; i *= 10) {
                for (int s = 0; s < 5; s++) {
                    ts.testMiliTime(i,s);
                }
                System.out.println("*******************************");
                System.out.println("*******************************");
                System.out.println("*******************************");
            }


    }

}
